/**
 * 
 */
package org.karen.droid.logviewtools.control;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.karen.droid.logviewtools.module.FileBean;
import org.karen.droid.logviewtools.module.LogBean;

import android.os.Environment;
import android.util.Log;

/**
 * @author Karen [Email:keyu404@gmail.com]
 * 
 */
public class FileHelper {
    private static final String TAG = "FileHelper";

    private static volatile Object lock = new Object();
    private static FileHelper INSTANCE = null;

    private FileHelper() {
    }

    public static FileHelper getInstance() {
        synchronized (lock) {
            if (null == INSTANCE) {
                FileHelper.INSTANCE = new FileHelper();
            }
        }
        return INSTANCE;
    }

    /**
     * Get the root of External SDCard.
     * 
     * @return {@link File} The External SDCard root.Or null if the external SDCard isn't exists or
     *         is unmounted.
     */
    public File getExSDCardRoot() {
        if (!Environment.isExternalStorageRemovable()) {
            return null;
        }
        String externalStatus = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(externalStatus)) {
            return Environment.getExternalStorageDirectory();
        } else {
            return null;
        }
    }

    public File getInSDCardRoot() {
        if (Environment.isExternalStorageRemovable()) {
            return null;
        }
        return Environment.getExternalStorageDirectory();
    }

    public File getSDCardRoot() {
        if (null != getExSDCardRoot()) {
            return getExSDCardRoot();
        } else {
            return getInSDCardRoot();
        }
    }

    /**
     * Get the files with in this file.
     * 
     * @param mFile
     *            The folder or file.
     * @return {@link List}The files in the folder.
     */
    public List<File> getFiles(File mFile) {
        if (null == mFile) {
            throw new NullPointerException("file not be null");
        }

        List<File> mList = new ArrayList<File>();

        if (mFile.isDirectory()) {
            File files[] = mFile.listFiles();
            if (null != files) {
                for (File f : files) {
                    Log.i(TAG, "File name =>>" + f.getName());
                    mList.add(f);
                }
            }
        }

        return mList;
    }

    public List<LogBean> getLogData(String filePath, int from, int count) {
        List<LogBean> mList = new ArrayList<LogBean>();
        BufferedReader bgr = null;
        try {
            bgr = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            String data = null;
            LogBean lb = null;
            int i = 0;
            while ((data = bgr.readLine()) != null) {
                lb = new LogBean();
                String datas[] = data.split("\\|");

                if (datas.length != 4) {
                    continue;
                }
                lb.date = datas[0];
                lb.level = datas[1];
                lb.tag = datas[2];
                lb.msg = datas[3];
                mList.add(lb);

                /*
                 * if (++i >= count) { break; }
                 */
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mList;
    }

}
